package cn.leejinki.admin.controller;

import cn.leejinki.admin.service.ContentService;
import cn.leejinki.admin.vo.ManageUserVO;
import cn.leejinki.constant.Const;
import cn.leejinki.pojo.TbCategorySecondary;
import cn.leejinki.pojo.LjkResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Random;


/**
 * Admin 首页Controller
 *
 * @author ljk.
 * @create 2018-04-11 下午3:38
 */

@Controller
public class AdminController {

    @Reference(version = Const.LJK_STORE_ADMIN_VERSION)
    private ContentService contentService;

    private Random random = new Random();

    @RequestMapping("/index")
    public String showIndex(Model model) {

        ManageUserVO userVO = new ManageUserVO();
        userVO.setCreated(new Date());
        userVO.setName("Leejinki");
        userVO.setJob("CEO");

        model.addAttribute("user", userVO);

        return "index";
    }
    @RequestMapping("/admin")
    public String showAdmin(Model model) {

        ManageUserVO userVO = new ManageUserVO();
        userVO.setCreated(new Date());
        userVO.setName("Leejinki");
        userVO.setJob("CEO");

        model.addAttribute("user", userVO);

        return "admin";
    }

    @RequestMapping("/show/logo")
    public String showLogo(Model model) {
        int nub = random.nextInt(60000);

        model.addAttribute("random", nub);

        return "editlogo";
    }
    @RequestMapping("/show/portal/menu/top")
    public String showPortalMenuTop(Model model) {
        int nub = random.nextInt(60000);

        model.addAttribute("random", nub);

        return "portal/menuTop";
    }
    @RequestMapping("/show/portal/menu/top/add")
    public String showPortalMenuTopAdd(Model model) {
        int nub = random.nextInt(60000);

        model.addAttribute("random", nub);
        return "portal/addMenuTop";
    }
    @RequestMapping("/portal/menu/top/getTableData")
    @ResponseBody
    public Map<String, Object> getPortalMenuTop(Model model, Integer sEcho, Integer iDisplayStart, Integer iDisplayLength) {

        Map<String, Object> lists = contentService.getPortalMenuTopList(sEcho,iDisplayStart,iDisplayLength);

        return lists;
    }
    @RequestMapping("/save/portal/menu/top")
    @ResponseBody
    public LjkResult savePortalMenuTop(String id, String name, Integer sort_order) {
        return contentService.savePortalMenuTop(id, name, sort_order);
    }
    @RequestMapping("/add/portal/menu/top")
    @ResponseBody
    public LjkResult addPortalMenuTop(String id, String name, Integer sort_order) {
        return contentService.addPortalMenuTop(id, name, sort_order);
    }
    @RequestMapping("/show/portal/tab/notice/add")
    public String showPortalTabNoticeAdd(Model model) {
        int nub = random.nextInt(60000);

        model.addAttribute("random", nub);
        return "portal/addTabNotice";
    }
    @RequestMapping("/show/editItem")
    public String showEditItem(Model model) {
        int nub = random.nextInt(60000);

        model.addAttribute("random", nub);

        return "editItem";
    }

    @RequestMapping("/show/addItem")
    public String showAddItem(Model model) {
        int nub = random.nextInt(60000);

        model.addAttribute("random", nub);
        int status=1;
        model.addAttribute("disabled",status);
        return "addItem";
    }
    //维护商品一级类目
    @RequestMapping("/show/oneCategory")
    public String showOneCategory(Model model) {
        int nub = random.nextInt(60000);
        model.addAttribute("random", nub);
        return "category";
    }
    @RequestMapping("/save/category")
    @ResponseBody
    public LjkResult saveCategory(String id, String name, Integer sort_order) {
        return contentService.saveCategory(id, name, sort_order);
    }
    //维护商品二级类目
    @RequestMapping("/show/twoCategory")
    public String showTwoCategory(Model model) {
        int nub = random.nextInt(60000);
        model.addAttribute("random", nub);
        return "twoCategory";
    }
    @RequestMapping("/save/category/secondary")
    @ResponseBody
    public LjkResult saveCategorySecondary(TbCategorySecondary categorySecondary) {
        return contentService.saveCategorySecondary(categorySecondary);
    }
    @RequestMapping("/category/getTableData")
    @ResponseBody
    public Map<String, Object> getCategory(Model model, Integer sEcho, Integer iDisplayStart, Integer iDisplayLength) {

        Map<String, Object> lists = contentService.getCategoryList(sEcho,iDisplayStart,iDisplayLength);

        return lists;
    }
    @RequestMapping("/category/secondary/getTableData")
    @ResponseBody
    public Map<String, Object> getCategorySecondary(Model model,String sSearch, Integer sEcho, Integer iDisplayStart, Integer iDisplayLength) {
        if (StringUtils.isNotBlank(sSearch)) {
            try {
                sSearch = new String(sSearch.getBytes("iso8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            Map<String, Object> lists = contentService.getSearchCategorySecondaryList(sSearch,sEcho,iDisplayStart,iDisplayLength);
            return lists;
        }
        Map<String, Object> lists = contentService.getCategorySecondaryList(sEcho,iDisplayStart,iDisplayLength);

        return lists;
    }
}
